#coding:utf-8

import re
import os
import pandas as pd

def read_file(fileName)->str:
    if not os.path.exists(fileName):
        return ""
    with open(fileName, 'r', encoding='utf-8') as f:
        content = f.read()
    return content

def parse_single_question(fileName):
    content = read_file(fileName)
    
    question_dict = {}

    questions = re.findall(r'(\d+)\.\s*(.*?)\s*\n((?:\([A-D]\)\s*.*?\n)+)', content, re.DOTALL)
    for question in questions:
        id = question[0]
        title = question[1]
        options_block = question[2]
         
        options = re.findall(r'\((A|B|C|D)\)\s*(.*?)\n', options_block)
         
        question_dict[id] = {
            "title": title,
            "options": options
        }
    
    return question_dict

def parse_single_question_to_excel(fileName):
    fileName = "单选题.txt"
    questions = parse_single_question(fileName) 
    
    data ={}

    for id,question in questions.items():
        
        title = question["title"]
        options = question["options"]
        a ,b,c,d = options 
        row = {
            "id": id,
            "title": title,
            "a": a[1],
            "b": b[1],
            "c": c[1],
            "d": d[1]
        }
        data[id] = row

    df = pd.DataFrame(data)
    # 行转列
    df = df.T
    df.to_excel("单选题.xlsx",index=False)

def parse_multiple_question_to_excel():
    fileName = "多选题.txt"
    content = read_file(fileName)
    question_dict = {}

    questions = re.findall(r'(\d+)\.\s*(.*?)\s*\n((?:\([A-E]\)\s*.*?\n)+)', content, re.DOTALL)
    for question in questions:
        id = question[0]
        title = question[1]
        options_block = question[2]
         
        options = re.findall(r'\((A|B|C|D|E)\)\s*(.*?)\n', options_block)
         
        question_dict[id] = {
            "title": title,
            "options": options
        }
    
    data ={}
    for id,question in question_dict.items():
        title = question["title"]
        options = question["options"]
        a ,b,c,d,e = options 
        row = {
            "id": id,
            "title": title,
            "a": a[1],
            "b": b[1],
            "c": c[1],
            "d": d[1],
            "e": e[1]
        }
        data[id] = row

    df = pd.DataFrame(data)
    df = df.T
    df.to_excel("多选题.xlsx",index=False)


if __name__ == "__main__":
    # parse_single_question_to_excel()
    parse_multiple_question_to_excel()
     
